<!--  -->
<template>
  <div>
    <el-dialog class="popa" :visible.sync="dialogVisible" width="70%" :before-close="handleClose">
      <el-form ref="form" :model="studentItem" label-width="90px">
        <el-card shadow="never" :body-style="cardPad">
          <div slot="header">
            <span>基本信息</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="姓名:">
                <el-input size="small" v-model="studentItem.name"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="性别:">
                <el-select size="small" v-model="studentItem.sex" placeholder="请选择性别">
                  <el-option label="男" value="0"></el-option>
                  <el-option label="女" value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="班级:">
                <el-select size="small" v-model="studentItem.classId" placeholder="请选择班级">
                  <el-option
                    v-for="item in classList"
                    :key="item.id"
                    :label="item.className"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="电话:">
                <el-input size="small" v-model="studentItem.tel"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="身份证号:">
                <el-input size="small" v-model="studentItem.manID"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="城市:">
                <el-cascader
                  size="large"
                  :options="options"
                  v-model="selectedOptions"
                  @change="handleChange"
                ></el-cascader>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="宿舍:">
                <el-select size="small" v-model="studentItem.dormId" placeholder="请选择宿舍">
                  <el-option
                    v-for="item in dormLists"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="入住日期:">
                <el-date-picker
                  size="small"
                  v-model="studentItem.dormStartTime"
                  type="date"
                  placeholder="选择入住日期"
                  value-format="yyyy-MM-dd"
                >></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="离开日期:">
                <el-date-picker
                  size="small"
                  v-model="studentItem.dormEndTime"
                  type="date"
                  placeholder="离开日期"
                  value-format="yyyy-MM-dd"
                >></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
        <el-card shadow="never" :body-style="cardPad">
          <div slot="header">
            <span>补充信息信息</span>
          </div>
          <el-row :gutter="20">
            <el-col :span="8">
              <el-form-item label="联系人电话:">
                <el-input size="small" v-model="studentItem.tel2"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="微信:">
                <el-input size="small" v-model="studentItem.wx"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="qq:">
                <el-input size="small" v-model="studentItem.qq"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="毕业院校:">
                <el-input size="small" v-model="studentItem.school"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="工作:">
                <el-input size="small" v-model="studentItem.job"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-card>
      </el-form>
      <el-card shadow="never" :body-style="cardPad">
        <div slot="header">
          <span>交费信息</span>
          <span class="cost" @click="fromShow=!fromShow">+添加交易信息</span>
        </div>
        <el-card v-if="fromShow" :body-style="cardPad" class="cost-from" shadow="never">
          <el-form :model="costItem" ref="form" label-width="80px">
            <el-row :gutter="20" style="margin-top:'100px' ">
              <el-col :span="8">
                <el-form-item label="类型:">
                  <el-radio-group v-model="costItem.costType">
                    <el-radio label="0">学费</el-radio>
                    <el-radio label="1">住宿费</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="金额:">
                  <el-input-number v-model="costItem.costValue" :min="0" :max="10000"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="交费时间:">
                  <el-date-picker
                    size="small"
                    v-model="costItem.costTime"
                    type="date"
                    placeholder="选择入学日期"
                    value-format="yyyy-MM-dd"
                  >></el-date-picker>
                </el-form-item>
              </el-col>
              <el-col :span="14">
                <el-form-item label="备注:">
                  <el-input
                    type="textarea"
                    style
                    :rows="2"
                    placeholder="备注信息"
                    v-model="costItem.costBak"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8" :offset="18">
                <el-form-item>
                  <el-button type="primary" size="small" @click="costSave">立即创建</el-button>
                  <el-button size="small" @click="fromShow=!fromShow">取消</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-table :data="costList" border stripe>
          <el-table-column type="index" width="80"></el-table-column>
          <el-table-column prop="costType" label="类型" width="130"></el-table-column>
          <el-table-column prop="costValue" label="金额" width="130"></el-table-column>
          <el-table-column prop="costTime" label="时间" width="200"></el-table-column>
          <el-table-column prop="costBak" label="备注" width="350"></el-table-column>
          <el-table-column fixed="right" label="操作" width="100" v-if="fromShow">
            <template slot-scope="scope">
              <el-button @click="delCost(scope.row.tempId)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>

      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
const _ = require("lodash");
import {
  regionDataPlus,
  CodeToText,
  TextToCode,
} from "element-china-area-data";
import { refreshData } from "@/utils/index";
import {
  studentList,
  studentSave,
  studentDetails,
  studentUpdate,
  studentCost,
} from "@/api/students/studentList";
import { set } from "js-cookie";
export default {
  name: "stuDetails",
  props: {
    classList: {
      type: Array,
    },
    dormLists: {
      type: Array,
    },
  },
  data() {
    return {
      options: regionDataPlus,
      selectedOptions: [],
      cardPad: { padding: "10px" },
      dialogVisible: false,
      costItem: {
        costType: "0",
      },
      fromShow: false,
      studentItem: {},
      costList: [],
      costListUp: [],
    };
  },
  methods: {
    removeList() {
      this.studentItem = {};
      this.costList = [];
      refreshData(this, "fromShow");
    },
    delCost(id) {
      this.costList = this.costList.filter((ele) => {
        return ele.tempId != id;
      });
    },
    costSave() {
      this.costItem.tempId = new Date().getTime();
      var obj = _.cloneDeep(this.costItem);
      var chong = true;
      this.costList.push(obj);
      this.costListUp.push(obj);
      refreshData(this, "costItem");
    },
    save() {
      var api = null;
      if (this.studentItem.id) {
        console.log(1);
        api = studentUpdate;
        var obj = {
          xyUser: {
            ...this.studentItem,
          },
          _size: this.costListUp.length,
        };
        for (var i = 0; i < this.costListUp.length; i++) {
          obj[`xyCostItems_${i}`] = {
            ...this.costListUp[i],
          };
        }
      } else {
        api = studentSave;
        var obj = {
          xyUser: {
            ...this.studentItem,
          },
          _size: this.costList.length,
        };

        for (var i = 0; i < this.costList.length; i++) {
          obj[`xyCostItems_${i}`] = {
            ...this.costList[i],
          };
        }
      }

      this.dialogVisible = false;
      api(obj).then((res) => {
        console.log(res);
        if (res.code == "success") {
          this.$emit("Refresh");
          this.costListUp = [];
        }
      });
    },
    openDetails(id) {
      refreshData(this, "fromShow");
      studentDetails({
        id,
      }).then((res) => {
        this.studentItem = res.data;
        studentCost({
          userId: res.data.id,
        }).then((res) => {
          this.costList = res.data.list;
          this.open();
        });
      });
    },
    handleChange(value) {
      var arr = [];
      value.forEach((ele) => {
        arr.push(CodeToText[ele]);
      });
      this.studentItem.city = value.toString(",");
      this.studentItem.cityText = arr.toString(",");
    },
    open() {
      this.costListUp = [];
      refreshData(this, "costItem");
      this.dialogVisible = true;
    },
    close() {
      this.dialogVisible = false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
  },
};
</script>

<style  lang='scss' scoped >
.popa {
  .el-form-item__label {
    font-weight: normal;
    font-size: 50px;
  }
  .el-card {
    margin-bottom: 20px;
    border: 0;
    .el-card.cost-from {
      margin-bottom: 10px;
    }
  }
  .cost {
    font-size: 12px;
    color: rgb(255, 145, 0);
    display: block;
    float: right;
    cursor: pointer;
  }
}
</style>
